using System;
using System.Collections.Generic;
using System.Text;

namespace modul1
{
   public class Boolean : Object
   {
        #region Constants
       private const int NUMBER_OF_BYTES_FOR_BOOLEAN = 1;
       #endregion

        #region Fields
        private bool? _value;
        #endregion

        #region Constructor
        public Boolean(bool? value)
        { 
            _value = value;
        }

        public Boolean(byte[] value)
        {
            FromByte(value);
        }
        #endregion

        #region Public Methods
       /// <summary>
       /// Converts current value to array of bytes.
       /// </summary>
       /// <returns></returns>
        public override byte[] ToByte()
        {
            byte[] buffer = new byte[NUMBER_OF_BYTES_FOR_BOOLEAN + 1];

            if (!this.IsNull())
            {
                byte[] tmp = BitConverter.GetBytes((bool)_value);
                buffer[0] = 1;

                for (int i = 0; i < tmp.Length; i++)
                {
                    buffer[i + 1] = tmp[i];
                }
            }
            else
            {
                for (int i = 0; i <= NUMBER_OF_BYTES_FOR_BOOLEAN; i++)
                {
                    buffer[i] = 0;
                }
            }

            return buffer;
        }

       /// <summary>
       /// Converts the array of bytes to Boolean value.
       /// </summary>
       /// <param name="buffer"></param>
        public override void FromByte(byte[] buffer)
        {
            if (buffer.Length < NUMBER_OF_BYTES_FOR_BOOLEAN + 1)
                throw new ApplicationException("Cannot convert to Boolean because of too short array of bytes");

            if (buffer[0] ==1)
            {
                if (buffer[1] > 1)
                    throw new ApplicationException("Invalid format of array of bytes");
                this._value = BitConverter.ToBoolean(buffer, 1);
            }
            else if (buffer[0] == 0)
                this._value = null;
            else
                throw new ApplicationException("Invalid first byte format in array of bytes");
        }

       /// <summary>
       /// Compare two instances of Boolean values.
       /// </summary>
       /// <param name="value"></param>
       /// <returns></returns>
        public override int Compare(modul1.Object val)
       {
           Boolean value = val as Boolean;
           if (value.IsNull() && this.IsNull())
               return 0;
           else if (value.IsNull() && !this.IsNull())
               return 1;
           else if (!value.IsNull() && this.IsNull())
               return -1;
           else if ((bool)this._value && !(bool)value._value)
               return 1;
           else if (!(bool)this._value && (bool)value._value)
               return -1;
           else
               return 0;
       }

       /// <summary>
       /// If current value is null then return True, else return false.
       /// </summary>
       /// <returns></returns>
        public override bool IsNull()
        {
            if (_value.HasValue)
                return false;
            else
                return true;
        }

       /// <summary>
       /// If value is type of Boolean return True, else return False.
       /// </summary>
       /// <param name="value"></param>
       /// <returns></returns>
       public static bool IsTypeOf(Object value)
       {
           System.Type type = value.GetType();

           if (type.FullName == "Structures.Boolean")
               return true;
           else
               return false;
       }

       /// <summary>
       /// Get number of bytes needed to Boolean type.
       /// </summary>
       /// <returns></returns>
        public static int GetLength()
        {
            return NUMBER_OF_BYTES_FOR_BOOLEAN + 1;
        }

       /// <summary>
       /// Converts current value to string.
       /// </summary>
       /// <returns></returns>
        public override string ToString()
        {
            if (!this.IsNull())
                return _value.ToString();
            else
                return string.Empty;
        }
       /// <summary>
       /// If value can be parsed to Boolean type return true, else return false.
       /// </summary>
       /// <param name="value"></param>
       /// <returns></returns>
       public static bool IsTypeOf(string value)
       {
           bool tmp;
           return bool.TryParse(value, out tmp);
       }
        #endregion
    }
}
